//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: IME.c 757 2007-05-10 08:19:31Z roger $
//

/* FILE : IME.c */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <standard_im.h>

#include "IME.h"
#include "im.h"

#define TEST_NAME "test1"
#define TEST_UUID "123456"
#define TEST_ENCODING "UTF-8"
#define TEST_CHARSET "unicode"
#define TEST_VERSION "1.0"

const char preedit[] = "preedit";
const char label0[] = "label0";
const char label1[] = "label1";
const char candidates0[] = "candidates0";
const char candidates1[] = "candidates1";

IME_String ime_string;
IME_Candidate ime_cand[2] = {
	{label0,	{candidates0, 0, NULL} },
	{label1,	{candidates1, 0, NULL} }
};
IME_CandidatesList ime_cands_list;

static IME_InputMethod im = {
	TEST_UUID,
	TEST_ENCODING,
	TEST_NAME,
	NULL,		//icon
	NULL,		//language
	NULL,		//helper_path
	NULL,		//help
	NULL,		//copyright
	TEST_VERSION
};

void
IME_initialize ()
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_ime_initialize()\n");
#endif
	char *keys[6] = {
		"/p0",
		"/p0/p1",
		"/p0/p2",
		"/p0/p3",
		"/p0/p3/p4",
		"/p0/p3/p5"
	};
	IME_PropertiesList property_list;
	IME_Property properties[6] = {
		{keys[0], keys[0], NULL, NULL, 0},
		{keys[1], keys[1], NULL, NULL, 0},
		{keys[2], keys[2], NULL, NULL, 0},
		{keys[3], keys[3], NULL, NULL, 0},
		{keys[4], keys[4], NULL, NULL, 0},
		{keys[5], keys[5], NULL, NULL, 0}
	};

	property_list.nr_properties = 6;
	property_list.properties = properties;

	immf->register_properties(TEST_UUID, &property_list);
	return;
}

void
IME_finalize (void)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_ime_finalize()\n");
#endif
	return;
}

int
IME_get_number_of_input_methods	(void)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : ime get nuber of input methods\n");
#endif
	return 1;
}

IME_Bool
IME_get_input_method_info (int index,
			   IME_InputMethod *info)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : ime get input method info\n");
#endif
	info->uuid = im.uuid;
	info->encoding = im.encoding;
	info->name = im.name;
	info->icon = im.icon;
	info->languages = im.languages;
	info->helper_path = im.helper_path;
	info->help = im.help;
	info->copyright = im.copyright;
	info->version = im.version;

	return IME_TRUE;
}

IME_Bool
IME_create_input_context (IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_create_input_context()\n");
#endif

	ic->private_data = NULL;

	return;
}

void
IME_destroy_input_context (IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_destroy_input_context()\n");
#endif
	if (ic->private_data)
		free(ic->private_data);
	return;
}

IME_Bool
IME_process_key_event (const IME_InputContext *ic,
		       const IME_KeyEvent *key)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_process_key_event()\n");
#endif

	const IME_String *const_string = &ime_string;
#if 0
	const IME_CandidatesList *const_cands_list = &ime_cands_list;
#else
	IM1Event imev;
#endif

	ime_string.string = preedit;
	ime_string.nr_attributes = 0;
	ime_string.attributes = NULL;

#if 0
	ime_cands_list.focused_candidate = 0;
	ime_cands_list.page_state = 0;
	ime_cands_list.nr_candidates = 2;
	ime_cands_list.candidates = ime_cand;
#endif

	if (key->code == '\b')
		return IME_FALSE;

	if (key->code == '\n' || key->code == 0x12 || key->code == 0x10 || key->code == 0x11)
		return IME_FALSE;

	if (key->ascii == '1' || key->ascii == ' ') {
		immf->hide_preedit_string(ic);
		immf->hide_candidates_list(ic);
		immf->commit_string(ic, ime_cand[0].content.string);
		return IME_TRUE;
	}
	if (key->ascii == '2') {
		immf->hide_preedit_string(ic);
		immf->hide_candidates_list(ic);
		immf->commit_string(ic, ime_cand[1].content.string);
		return IME_TRUE;
	}

	immf->update_preedit_string(ic, const_string);
	immf->show_preedit_string(ic);
#if 0
	immf->update_candidates_list(ic, const_cands_list);
	immf->show_candidates_list(ic);
#else
	bzero(&imev, sizeof(IM1Event));
	imev.type = IM1_CANDIDATE;
	imev.num = 2;
	strcpy(imev.string0, candidates0);
	strcpy(imev.string1, candidates1);
	immf->send_helper_event(ic, TEST_UUID, (void *)&imev, sizeof(IM1Event));
#endif

	return IME_TRUE;
}

void
IME_process_helper_event (const IME_InputContext *ic,
		const char *helper_uuid,
		const void *buf,
		size_t buf_size)
{
	return;
}

void
IME_focus_in (const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_focus_in()\n");
#endif
	return;
}

void
IME_focus_out (const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_focus_out()\n");
#endif
	return;
}

void
IME_reset (const IME_InputContext   *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_reset()\n");
#endif
	return;
}

void
IME_move_preedit_caret (const IME_InputContext   *ic, int position)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_move_preedit_caret\n");
#endif
	return;
}
 
void
IME_select_candidate (const IME_InputContext   *ic, int index)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_select_candidate\n");
#endif
	return;
}

void
IME_previous_candidates_page (const IME_InputContext *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_previous_candidates_page\n");
#endif
	return;
}

void
IME_next_candidates_page (const IME_InputContext   *ic)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_next_candidates_page\n");
#endif
	return;
}

void
IME_update_candidates_page_size (const IME_InputContext   *ic, int size)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_update_candidates_size\n");
#endif
	return;
}

void
IME_trigger_property (const IME_InputContext *ic, const char *property)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_trigger_property\n");
#endif
	return;
}

void
IME_reload_config (void)
{
#if DEBUG
	fprintf(stderr, "STD IM TEST1 : IME_reload_config\n");
#endif
	return;
}

// vi:ts=4:nowrap:cin:expandtab
